package com.ruoyi.base.service.impl;

import java.util.*;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.BaseRiverMapper;
import com.ruoyi.base.domain.BaseRiver;
import com.ruoyi.base.service.IBaseRiverService;
import org.springframework.util.CollectionUtils;

/**
 * 河流Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-27
 */
@Service
public class BaseRiverServiceImpl implements IBaseRiverService {
    @Autowired
    private BaseRiverMapper baseRiverMapper;

    /**
     * 查询河流
     *
     * @param ID 河流主键
     * @return 河流
     */
    @Override
    public BaseRiver selectBaseRiverByID(String ID) {
        return baseRiverMapper.selectBaseRiverByID(ID);
    }

    /**
     * 查询河流列表
     *
     * @param baseRiver 河流
     * @return 河流
     */
    @Override
    public List<BaseRiver> selectBaseRiverList(BaseRiver baseRiver) {
        return baseRiverMapper.selectBaseRiverList(baseRiver);
    }

    /**
     * 新增河流
     *
     * @param baseRiver 河流
     * @return 结果
     */
    @Override
    public int insertBaseRiver(BaseRiver baseRiver) {
        String id = UUID.randomUUID().toString();
        baseRiver.setId(id);
        baseRiver.setCreateTime(new Date());
        String parentRiverId = baseRiver.getParentriverid();
        String parentallid = baseRiver.getParentallid();

        //如果父级河流为空，则赋值当前id，否则查找父级id的顶级id
        if (parentRiverId != null && !"".equals(parentRiverId)) {
            //查找父级的顶级河流id
            BaseRiver parbaseRiver = new BaseRiver();
            parbaseRiver.setId(parentRiverId);
            List<BaseRiver> baseRivers = baseRiverMapper.selectBaseRiverList(parbaseRiver);

            baseRiver.setTopriverid(baseRivers.get(0).getTopriverid());
            baseRiver.setToprivercode(baseRivers.get(0).getToprivercode());
            baseRiver.setToprivername(baseRivers.get(0).getToprivername());
        } else {
            baseRiver.setTopriverid(id);
            baseRiver.setToprivercode(baseRiver.getRivercode());
            baseRiver.setToprivername(baseRiver.getRivername());
        }

        //如果父级id也是空也赋值当前id
        if (parentRiverId != null && !"".equals(parentRiverId)) {
            BaseRiver parbaseRiver = new BaseRiver();
            parbaseRiver.setParentriverid(parentRiverId);
            List<BaseRiver> baseRivers = baseRiverMapper.selectBaseRiverList(parbaseRiver);
            if (baseRivers != null && baseRivers.size() > 0) {
                BaseRiver baseRiver1 = baseRivers.get(0);
                String parentallid1 = baseRiver1.getParentallid();
                String allid = null;
                if (parentallid1 != null && !"".equals(parentallid1) && !"null".equals(parentallid1)) {
                    allid = parentallid1 + "," + id;
                } else {
                    allid = id;
                }
                // String allid = parentallid1 + "," + id;
                List<String> parentallids = new ArrayList<>();
                if (allid != null && !"".equals(allid)) {
                    String[] arr = allid.split(",");
                    parentallids = Arrays.asList(arr);
                }
                String parentallidall = null;
                List<String> parentallidalllist = new ArrayList<>();
                if (parentallids != null && parentallids.size() > 0) {
                    for (String datid : parentallids) {
                        if (parentallidall != null && !"".equals(parentallidall)) {
                            boolean contains = parentallidalllist.contains(datid);
                            if (contains) {
                            } else {
                                parentallidalllist.add(datid);
                                parentallidall = parentallidall + "," + datid;
                            }
                        } else {
                            boolean contains = parentallidalllist.contains(datid);
                            if (contains) {
                            } else {
                                parentallidalllist.add(datid);
                                parentallidall = datid;
                            }

                        }
                    }
                }
                baseRiver.setParentallid(parentallidall);
            } else {
                if (parentallid != null && !"".equals(parentallid) && !"null".equals(parentallid)) {
                    String allid = parentallid + "," + id;
                    List<String> parentallids = new ArrayList<>();
                    if (allid != null && !"".equals(allid)) {
                        String[] arr = allid.split(",");
                        parentallids = Arrays.asList(arr);
                    }
                    String parentallidall = null;
                    List<String> parentallidalllist = new ArrayList<>();
                    if (parentallids != null && parentallids.size() > 0) {
                        for (String datid : parentallids) {
                            if (parentallidall != null && !"".equals(parentallidall)) {
                                boolean contains = parentallidalllist.contains(datid);
                                if (contains) {
                                } else {
                                    parentallidalllist.add(datid);
                                    parentallidall = parentallidall + "," + datid;
                                }
                            } else {
                                boolean contains = parentallidalllist.contains(datid);
                                if (contains) {
                                } else {
                                    parentallidalllist.add(datid);
                                    parentallidall = datid;
                                }

                            }
                        }
                    }
                    baseRiver.setParentallid(parentallidall);
                } else {
                    baseRiver.setParentallid(id);
                }
            }
        } else {
            baseRiver.setParentriverid(id);
            baseRiver.setParentrivercode(baseRiver.getRivercode());
            baseRiver.setParentrivername(baseRiver.getRivername());
            if (parentallid != null && !"".equals(parentallid) && !"null".equals(parentallid)) {
                String allid = parentallid + "," + id;
                List<String> parentallids = new ArrayList<>();
                if (allid != null && !"".equals(allid)) {
                    String[] arr = allid.split(",");
                    parentallids = Arrays.asList(arr);
                }
                String parentallidall = null;
                List<String> parentallidalllist = new ArrayList<>();
                if (parentallids != null && parentallids.size() > 0) {
                    for (String datid : parentallids) {
                        if (parentallidall != null && !"".equals(parentallidall) && !"null".equals(parentallid)) {
                            boolean contains = parentallidalllist.contains(datid);
                            if (contains) {
                            } else {
                                parentallidalllist.add(datid);
                                parentallidall = parentallidall + "," + datid;
                            }
                        } else {
                            boolean contains = parentallidalllist.contains(datid);
                            if (contains) {
                            } else {
                                parentallidalllist.add(datid);
                                parentallidall = datid;
                            }

                        }
                    }
                }
                baseRiver.setParentallid(parentallidall);
            } else {
                baseRiver.setParentallid(id);
            }
        }
        return baseRiverMapper.insertBaseRiver(baseRiver);
    }

    /**
     * 修改河流
     *
     * @param baseRiver 河流
     * @return 结果
     */
    @Override
    public int updateBaseRiver(BaseRiver baseRiver) {
        baseRiver.setUpdateTime(new Date());
        String parentRiverId = baseRiver.getParentriverid();
        String parentallid = baseRiver.getParentallid();
        String id = baseRiver.getId();
        //如果父级河流为空，则赋值当前id，否则查找父级id的顶级id
        if (parentRiverId != null && !"".equals(parentRiverId)) {
            //查找父级的顶级河流id
            BaseRiver parbaseRiver = new BaseRiver();
            parbaseRiver.setParentriverid(parentRiverId);
            List<BaseRiver> baseRivers = baseRiverMapper.selectBaseRiverList(parbaseRiver);

            baseRiver.setTopriverid(baseRivers.get(0).getTopriverid());
            baseRiver.setToprivercode(baseRivers.get(0).getToprivercode());
            baseRiver.setToprivername(baseRivers.get(0).getToprivername());
        } else {
            baseRiver.setTopriverid(id);
            baseRiver.setToprivercode(baseRiver.getRivercode());
            baseRiver.setToprivername(baseRiver.getRivername());
        }

        //如果父级id也是空也赋值当前id
        if (parentRiverId != null && !"".equals(parentRiverId)) {
            BaseRiver parbaseRiver = new BaseRiver();
            parbaseRiver.setParentriverid(parentRiverId);
            List<BaseRiver> baseRivers = baseRiverMapper.selectBaseRiverList(parbaseRiver);
            if (baseRivers != null && baseRivers.size() > 0) {
                BaseRiver baseRiver1 = baseRivers.get(0);
                String parentallid1 = baseRiver1.getParentallid();
                String allid = null;
                if (parentallid1 != null && !"".equals(parentallid1) && !"null".equals(parentallid1)) {
                    allid = parentallid1 + "," + id;
                } else {
                    allid = id;
                }
                // String allid = parentallid1 + "," + id;
                List<String> parentallids = new ArrayList<>();
                if (allid != null && !"".equals(allid)) {
                    String[] arr = allid.split(",");
                    parentallids = Arrays.asList(arr);
                }
                String parentallidall = null;
                List<String> parentallidalllist = new ArrayList<>();
                if (parentallids != null && parentallids.size() > 0) {
                    for (String datid : parentallids) {
                        if (parentallidall != null && !"".equals(parentallidall)) {
                            boolean contains = parentallidalllist.contains(datid);
                            if (contains) {
                            } else {
                                parentallidalllist.add(datid);
                                parentallidall = parentallidall + "," + datid;
                            }
                        } else {
                            boolean contains = parentallidalllist.contains(datid);
                            if (contains) {
                            } else {
                                parentallidalllist.add(datid);
                                parentallidall = datid;
                            }

                        }
                    }
                }
                baseRiver.setParentallid(parentallidall);
            } else {
                if (parentallid != null && !"".equals(parentallid) && !"null".equals(parentallid)) {
                    String allid = parentallid + "," + id;
                    List<String> parentallids = new ArrayList<>();
                    if (allid != null && !"".equals(allid)) {
                        String[] arr = allid.split(",");
                        parentallids = Arrays.asList(arr);
                    }
                    String parentallidall = null;
                    List<String> parentallidalllist = new ArrayList<>();
                    if (parentallids != null && parentallids.size() > 0) {
                        for (String datid : parentallids) {
                            if (parentallidall != null && !"".equals(parentallidall)) {
                                boolean contains = parentallidalllist.contains(datid);
                                if (contains) {
                                } else {
                                    parentallidalllist.add(datid);
                                    parentallidall = parentallidall + "," + datid;
                                }
                            } else {
                                boolean contains = parentallidalllist.contains(datid);
                                if (contains) {
                                } else {
                                    parentallidalllist.add(datid);
                                    parentallidall = datid;
                                }

                            }
                        }
                    }
                    baseRiver.setParentallid(parentallidall);
                } else {
                    baseRiver.setParentallid(id);
                }
            }
        } else {
            baseRiver.setParentriverid(id);
            baseRiver.setParentrivercode(baseRiver.getRivercode());
            baseRiver.setParentrivername(baseRiver.getRivername());
            if (parentallid != null && !"".equals(parentallid) && !"null".equals(parentallid)) {
                String allid = parentallid + "," + id;
                List<String> parentallids = new ArrayList<>();
                if (allid != null && !"".equals(allid)) {
                    String[] arr = allid.split(",");
                    parentallids = Arrays.asList(arr);
                }
                String parentallidall = null;
                List<String> parentallidalllist = new ArrayList<>();
                if (parentallids != null && parentallids.size() > 0) {
                    for (String datid : parentallids) {
                        if (parentallidall != null && !"".equals(parentallidall) && !"null".equals(parentallid)) {
                            boolean contains = parentallidalllist.contains(datid);
                            if (contains) {
                            } else {
                                parentallidalllist.add(datid);
                                parentallidall = parentallidall + "," + datid;
                            }
                        } else {
                            boolean contains = parentallidalllist.contains(datid);
                            if (contains) {
                            } else {
                                parentallidalllist.add(datid);
                                parentallidall = datid;
                            }

                        }
                    }
                }
                baseRiver.setParentallid(parentallidall);
            } else {
                baseRiver.setParentallid(id);
            }
        }
        return baseRiverMapper.updateBaseRiver(baseRiver);
    }

    /**
     * 批量删除河流
     *
     * @param IDs 需要删除的河流主键
     * @return 结果
     */
    @Override
    public int deleteBaseRiverByIDs(String[] IDs) {
        return baseRiverMapper.deleteBaseRiverByIDs(IDs);
    }

    /**
     * 删除河流信息
     *
     * @param ID 河流主键
     * @return 结果
     */
    @Override
    public int deleteBaseRiverByID(String ID) {
        return baseRiverMapper.deleteBaseRiverByID(ID);
    }

    @Override
    public List<Map> treeList() {
        List<Map> mapList = new ArrayList<>();
        List<BaseRiver> baseRiverList = baseRiverMapper.selectBaseRiverList(new BaseRiver());
        if(!CollectionUtils.isEmpty(baseRiverList)){
            for (BaseRiver baseRiver : baseRiverList) {
                String pid = "0";
                Map map = new HashMap();
                map.put("id",baseRiver.getId());
                if(!StringUtils.isEmpty(baseRiver.getParentriverid()) && !baseRiver.getParentriverid().equals(baseRiver.getId())){
                    pid = baseRiver.getParentriverid();
                }
                map.put("pId",pid);
                map.put("name",baseRiver.getRivername());
                mapList.add(map);
            }
        }
        return mapList;
    }
}
